import 'package:flutter/material.dart';
import 'package:nihao_flutter/model/post.dart';


class PostDataSource extends DataTableSource{
    
    final List<Post>_posts = posts;
    int _selectedCount = 0;

    @override
  // TODO: implement rowCount
  int get rowCount => _posts.length;
  @override
  // TODO: implement isRowCountApproximate
  bool get isRowCountApproximate => false;

  @override
  // TODO: implement selectedRowCount
  int get selectedRowCount => _selectedCount;

  @override
  DataRow getRow(int index) {
    // TODO: implement getRow

    return DataRow.byIndex(
      index: index,
      cells: <DataCell>[

        DataCell(Text(_posts[index].title)),
        DataCell(Text(_posts[index].author)),
        DataCell(
          Image.network(_posts[index].imageUrl,
          fit: BoxFit.cover)
          ),

    ]);
    
  }

  void _sort(getField(post),bool ascending){
    _posts.sort((a,b){
       if (!ascending) {
         final c=a;
         a=b;
         b=c;
       }
      final aValue = getField(a);
      final bValue = getField(b);
      
      return Comparable.compare(aValue, bValue);
       
    });
   
   notifyListeners();

  }

}

class PaginatedDataTableDemo extends StatefulWidget {
  @override
  _PaginatedDataTableDemo createState() => _PaginatedDataTableDemo();
}

class _PaginatedDataTableDemo extends State<PaginatedDataTableDemo> {
  int _sortColumnIndex=0;
  bool _sortAssending = true;

  PostDataSource _postsDataSource = PostDataSource();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title:Text('PaginatedDataTableDemo'),
          elevation: 0.0,
        ),
         body: Container(
         padding: EdgeInsets.all(10),
         child: ListView(
           children: <Widget>[
             PaginatedDataTable(
               header: Text('Posts'),
               rowsPerPage: 5,
               source: _postsDataSource,
               sortColumnIndex: _sortColumnIndex,
               sortAscending: _sortAssending,
               columns: [
                 DataColumn(
                   label:Container(
                      child: Text('Title'),
                   ),
                   onSort: (int index, bool ascending){
                      _postsDataSource._sort((post) => post.title.length, ascending);
                        setState(() {
                          _sortColumnIndex = index;
                          _sortAssending = ascending;
                        });
                   },
                  ),
                  DataColumn(
                   label:Container(
                      child: Text('Author'),
                     // width: 150,
                   )
                  ),
                  DataColumn(
                  label:Container(
                      child: Text('Image'),
                     // width: 150,
                   )
                  ),
               ]
               )
           ],

         )
        ),
    );
  }
  }